package com.leenleda.ips.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.os.Bundle;
import android.os.Environment;
import android.os.Looper;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.leenleda.ips.R;
import com.leenleda.ips.databinding.ActivityStrBinding;
import com.leenleda.ips.util.LogUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class StrActivity extends AppCompatActivity {

    private ActivityStrBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window _window = getWindow();
        WindowManager.LayoutParams params = _window.getAttributes();
//        params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_IMMERSIVE;
        params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
        _window.setAttributes(params);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);// 隐藏状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);//禁用键盘

        binding = DataBindingUtil.setContentView(this, R.layout.activity_str);

        String text = getStrData();
        LogUtils.e("getStrData", "====text===22=" + text);
        binding.textView.setText(text);
    }

    /**
     * 右上角返回键
     */
    public void btnBack(View view){
        finish();
    }

    private String getStrData() {
        StringBuilder text = new StringBuilder();
        String name = getIntent().getStringExtra("name");
        String path = null;
        try {
            path = Environment.getExternalStorageDirectory().getCanonicalPath() + "/" + "LELDIPS/Crash/" + name;
            File file = new File(path);
            if (file == null)
                return "";
            InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
            BufferedReader br = new BufferedReader(isr);

            String mimeTypeLine = null ;
            while ((mimeTypeLine = br.readLine()) != null) {
                LogUtils.e("getStrData", "====while===1=" + mimeTypeLine);
                text.append("\n");
                text.append(mimeTypeLine);
//                text = text + "\n" +mimeTypeLine;
//                text = text + mimeTypeLine;
            }
//            ILog.e("getStrData", "====text===1=" + text);
            return text.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    private void getDa(){
        Process mLogcatProc = null;
        BufferedReader reader = null;
        try {
            //获取logcat日志信息
            mLogcatProc = Runtime.getRuntime().exec(new String[] { "logcat","Mytest:I *:S" });
            reader = new BufferedReader(new InputStreamReader(mLogcatProc.getInputStream()));

            String line;

            while ((line = reader.readLine()) != null) {
                LogUtils.e("getStrData", "====line===1=" + line);

                if (line.indexOf("this is a test") > 0) {
                    //logcat打印信息在这里可以监听到
                    // 使用looper 把给界面一个显示
                    Looper.prepare();
//                    Toast.makeText(this, "监听到log信息", Toast.LENGTH_SHORT).show();
                    Looper.loop();
                }
            }

        } catch (Exception e) {

            e.printStackTrace();
        }
    }
}
